<!DOCTYPE html>
<html>
<head>
    <title>Thermodynamics Process Diagram</title>
</head>
<body>
<canvas id="physicsCanvas" width="650" height="280"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // --- Configuration ---
    ctx.lineWidth = 2;
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    const mainFont = "26px sans-serif";
    const supFont = "17px sans-serif";
    const labelFont = "bold 26px sans-serif";
    const supYOffset = -10;

    // --- Draw Boxes ---
    const boxW = 260;
    const boxH = 130;
    const boxY = 40;
    const leftBoxX = 30;
    const rightBoxX = 360;
    ctx.strokeRect(leftBoxX, boxY, boxW, boxH);
    ctx.strokeRect(rightBoxX, boxY, boxW, boxH);

    // --- Draw Arrow ---
    const arrowY = boxY + boxH / 2;
    const arrowStartX = leftBoxX + boxW;
    const arrowEndX = rightBoxX;
    
    ctx.beginPath();
    ctx.moveTo(arrowStartX, arrowY);
    ctx.lineTo(arrowEndX, arrowY);
    ctx.stroke();

    const arrowHeadCenterX = (arrowStartX + arrowEndX) / 2;
    const arrowHeadTipX = arrowHeadCenterX + 10;
    const arrowHeadBackX = arrowHeadCenterX - 10;
    const arrowHeadHalfHeight = 10;
    
    ctx.beginPath();
    ctx.moveTo(arrowHeadBackX, arrowY - arrowHeadHalfHeight);
    ctx.lineTo(arrowHeadTipX, arrowY);
    ctx.lineTo(arrowHeadBackX, arrowY + arrowHeadHalfHeight);
    ctx.stroke();

    // --- Helper function for positioning text with superscripts ---
    function drawCenteredTextWithSuperscripts(centerX, y, parts) {
        // Calculate total width of the text for centering
        let totalWidth = 0;
        parts.forEach(p => {
            ctx.font = p.isSup ? supFont : mainFont;
            totalWidth += ctx.measureText(p.text).width;
        });

        // Set starting X to draw from left to right
        let currentX = centerX - totalWidth / 2;
        ctx.textAlign = 'left';
        ctx.textBaseline = 'middle';

        // Draw each part of the text
        parts.forEach(p => {
            ctx.font = p.isSup ? supFont : mainFont;
            const currentY = p.isSup ? y + supYOffset : y;
            ctx.fillText(p.text, currentX, currentY);
            currentX += ctx.measureText(p.text).width;
        });
    }

    // --- Draw Text in Boxes ---
    const leftBoxCenterX = leftBoxX + boxW / 2;
    const rightBoxCenterX = rightBoxX + boxW / 2;
    const yLine1 = boxY + 35;
    const yLine2 = boxY + 70;
    const yLine3 = boxY + 105;

    // Content for the left box
    const textVol = [
        { text: '2.40 x 10', isSup: false },
        { text: '3', isSup: true },
        { text: ' cm', isSup: false },
        { text: '3', isSup: true }
    ];
    const textP1 = [
        { text: '3.51 x 10', isSup: false },
        { text: '5', isSup: true },
        { text: ' Pa', isSup: false }
    ];
    drawCenteredTextWithSuperscripts(leftBoxCenterX, yLine1, textVol);
    drawCenteredTextWithSuperscripts(leftBoxCenterX, yLine2, textP1);
    ctx.font = mainFont;
    ctx.textAlign = 'center';
    ctx.fillText('290 K', leftBoxCenterX, yLine3);

    // Content for the right box
    const textP2 = [
        { text: '3.75 x 10', isSup: false },
        { text: '5', isSup: true },
        { text: ' Pa', isSup: false }
    ];
    drawCenteredTextWithSuperscripts(rightBoxCenterX, yLine1, textVol); // Volume is the same
    drawCenteredTextWithSuperscripts(rightBoxCenterX, yLine2, textP2);
    ctx.font = mainFont;
    ctx.textAlign = 'center';
    ctx.fillText('310 K', rightBoxCenterX, yLine3);

    // --- Draw Figure Label ---
    const labelY = boxY + boxH + 60;
    ctx.font = labelFont;
    ctx.textAlign = 'center';
    ctx.fillText('Fig. 2.1', canvas.width / 2, labelY);
</script>
</body>
</html>